package cn.bdqn.t354.dao;

import cn.bdqn.t354.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.util.List;

public interface UserDao extends JpaRepository<User,Long>,
        JpaSpecificationExecutor<User> {

    /**
     * 根据角色id查询所有的用户信息
     * @param roleId
     * @return
     */
//    @Query("from User where usrRoleId=?1")
    @Query("from User where usrRoleId=:roleId")
    public List<User> findAllUsersByRoleId(@Param("roleId") Long roleId);

    //修改，删除
    @Transactional
    @Modifying
    @Query("update User set usrName=?1 where usrId=?2")
    public void updateUserByUserId(String usrName,Long usrId);


    @Query(value = "select * from sys_user where usr_role_id=?1",
            nativeQuery = true)
    public List<User> findAllUsersByRoleId2(Long roleId);

    /**
     * 根据方法名规则查询 默认精确匹配
     * @param usrRoleId
     * @return
     */
    public List<User> findByUsrRoleId(Long usrRoleId);

    /**
     * 模糊查询
     * @param usrName
     * @return
     */
    public List<User> findByUsrNameLike(String usrName);

    /**
     * 多条件查询
     * @param usrName
     * @param password
     * @return
     */
    public User findByUsrNameAndUsrPassword(String usrName,String password);
}
